﻿using System;
using System.Collections.Generic;
using System.Text;
using Acms.Bll;
using System.Collections;

namespace Acms.DataControls
{
    [System.ComponentModel.Designer(typeof(CustomAdminPageDataSourceDesigner))]
    [System.Web.UI.ToolboxData("<{0}:ControlTypeDataSource runat=server/>")]
    public class CustomAdminPageDataSource : GenericDataSource<CustomAdminPage>
    {
        public enum Fields
        {
            Id,
            SiteId,
            Name,
            Assembly,
            ControlPath,
            Notes,
            Sequence,
            Section,
            ConcurencyId
        }

        protected override CustomAdminPage PopulateData(UpdateAction action, IDictionary keys, IDictionary values, IDictionary oldValues)
        {
            CustomAdminPage tmp = null;
            byte[] concurencyId = null;

            if (action == UpdateAction.Insert)
            {
                int siteId = GetValue<int>(Fields.SiteId, values);
                
                if (siteId == 0)
                {
                    throw new ApplicationException("SiteId has not been set in the values collection. Must be set to allow inserting usually in the BeforeUpdateing Event.");
                }
                tmp = CustomAdminPage.CreateInstance(siteId);
            }
            else
            {
                int id = GetValue<int>(Fields.Id, keys);
                concurencyId = GetValue<byte[]>(Fields.ConcurencyId, keys);
                tmp = CustomAdminPage.GetById(id);
            }

            if (tmp != null)
            {
                tmp.CheckConcurent(concurencyId);

                if (RequiresUpdate<string>(Fields.Name, values, oldValues))
                {
                    tmp.Name = GetValue<string>(Fields.Name, values);
                }

                if (RequiresUpdate<string>(Fields.ControlPath, values, oldValues))
                {
                    tmp.ControlPath = GetValue<string>(Fields.ControlPath, values);
                }

                if (RequiresUpdate<string>(Fields.Notes, values, oldValues))
                {
                    tmp.Notes = GetValue<string>(Fields.Notes, values);
                }

                if (RequiresUpdate<Acms.Bll.CustomAdminPage.Sections>(Fields.Section, values, oldValues))
                {
                    tmp.Section = GetValue<Acms.Bll.CustomAdminPage.Sections>(Fields.Section, values);
                }
            }
            return tmp;
        }

        protected override int SaveChanges(CustomAdminPage item)
        {
            item.Update();
            return 1;
        }

        protected override int Delete(CustomAdminPage item)
        {
            item.Delete();
            return 1;
        }



    }

    public class CustomAdminPageDataSourceDesigner : GenericDataSourceDesigner<CustomAdminPage>
    {
        protected override CustomAdminPage GetDesignTimeItem(int index)
        {
            Random rnd = new Random(index);
            string[] names = new string[]{"Average Amounts", "Newsletter SignUps", "Coulurs"};
            CustomAdminPage tmp = CustomAdminPage.CreateInstance(index);
            tmp.Name = names[rnd.Next(0, names.Length)];
            return tmp;
        }
    }
}
